package com.xm.settlement.controller;

import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.model.NewsManage;
import com.xm.settlement.service.INewsManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @description：公告管理
 * @author：tantao
 * @date：2017/03/31
 */
@Controller
@RequestMapping("/newsManage")
public class NewsManageController extends BaseController {

    @Autowired
    private INewsManageService newsManageService;

    /**
     * 公告管理主页
     *
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "serviceCenter/newsManage";
    }

     /**
     * 公告管理列表
     *
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        Long createId = getUserId();
        condition.put("createId",createId);
        pageInfo.setCondition(condition);
        newsManageService.selectDataGrid(pageInfo);
        return pageInfo;
    }

    /**
     * 添加公告管理页
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "serviceCenter/newsManageAdd";
    }


    /**
     * 添加公告管理
     *
     * @param newsManage
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(NewsManage newsManage ) {
        newsManage.setCreateId(getUserId());
        newsManage.setCreateTime(new Date());
        newsManageService.insert(newsManage);
        return renderSuccess("添加成功！");
}

    /**
     * 编辑公告管理页
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        NewsManage newsManage = newsManageService.selectById(id);
        model.addAttribute("newsManage", newsManage);
        return "serviceCenter/newsManageEdit";
    }

    /**
     * 编辑公告管理
     *
     * @param   newsManage
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(NewsManage newsManage) {
        newsManage.setUpdateTime(new Date());
        newsManage.setUpdateId(getUserId());
        newsManageService.updateSelectiveById(newsManage);
        return renderSuccess("编辑成功！");
    }
    /**
     * 删除公告管理
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        newsManageService.deleteById(id);
        return renderSuccess("删除成功！");
    }


    /**
     * 发布公告管理
     *
     * @param id
     * @return
     */
    @RequestMapping("/release")
    @ResponseBody
    public Object release(Long id) {
        NewsManage update = newsManageService.selectById(id);
        update.setStatus(1);
        newsManageService.updateSelectiveById(update);
        return renderSuccess("发布成功！");
    }

/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/

    /**
     * 查看公告主页
     *
     * @return
     */
    @GetMapping("/manager1")
    public String manager1() {
        return "serviceCenter/newsManage1";
    }


    /**
     * 查看公告列表
     * @return
     */
    @PostMapping("/dataGrid1")
    @ResponseBody
    public Object dataGrid1(Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        newsManageService.selectDataGrid1(pageInfo);
        return pageInfo;
    }


    /**
     * 展示公告内容
     */
    @RequestMapping("/display")
    public String displayPage(Model model, Long id) {
        NewsManage newsManage = newsManageService.selectById(id);
        model.addAttribute("newsManage", newsManage);
        return "serviceCenter/newsManageDisplay";
    }

}
